/*
 * 文件名：SettingController.java
 * 版权：
 * 描述：TODO
 * 修改人：zuo liubo
 * 修改时间：2013-3-27 下午2:44:29
 */
package com.scpii.api.controller.push;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scpii.api.common.base.BaseController;
import com.scpii.api.common.exception.UserIdDisaccordException;
import com.scpii.api.domain.auth.UserConfigs;
import com.scpii.api.domain.auth.UsersInfoDetails;
import com.scpii.api.service.auth.DeviceService;
import com.scpii.api.service.user.UserConfigService;
import com.scpii.api.util.PrincipalUtil;

/**
 * @description 消息中心设置
 * @author zuo liuboO
 */
@Controller
@RequestMapping("/push/setting")
public class SettingController extends BaseController{

	@Resource
	private UserConfigService userConfigService;
	@Resource
	private DeviceService deviceService;
	
	/**
	 * 设置用户接收push消息的设置
	 * @param userId
	 * @param userConfigs
	 * @return
	 * @throws Exception
	 */
	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.POST)
	@ResponseBody
	public Object settingPush(@PathVariable("userId") Long userId,UserConfigs userConfigs) throws Exception{
		UsersInfoDetails  u = PrincipalUtil.getPrincipal();
		Long _userId = u.getId();
		if (!_userId.equals(userId)) {
			throw new UserIdDisaccordException();
		}
		userConfigs.setId(userId);
		return userConfigService.updatePushSetting(userConfigs);
	}
	
	/**
	 * 获取用户PUSH消息设置
	 * @param userId
	 * @param userConfigs
	 * @return
	 * @throws Exception
	 */
	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
	@ResponseBody
	public Object getSettingPush(@PathVariable("userId") Long userId) throws Exception{
		return userConfigService.selectUserConfigs(userId);
	}
	
	/**
	 * 设置IOS设备的token
	 * @param iosToken
	 * @return
	 * @throws Exception
	 */
	@PreAuthorize("hasRole('ROLE_CLIENT')")
	@RequestMapping(value = "/iosToken", method = RequestMethod.POST)
	@ResponseBody
	public Object updateIOSToken(String iosToken) throws Exception{
		UsersInfoDetails  u = PrincipalUtil.getPrincipal();
		Long deviceId = u.getDeviceId();
		return deviceService.updateIOSToken(deviceId, iosToken);
	}
}
